<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange">
      <template #formName="{ row, type, size }">
        <ElTag style="cursor: pointer" @click="approvalClick(row, type, size)">{{ row.formName }}</ElTag>
      </template>
    </FyCurd>
  </BasicContainer>
</template>

<script>
  import { getAppingTasksPagerModel } from '@/api/admin/workitem'
  import { tableOptionTodo } from '@/const/crud/admin/todo'
  import { mapState } from 'vuex'

  export default {
    name: 'Todo',
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOptionTodo
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {}
      }
    },
    methods: {
      // 列表查询
      getList(page, params) {
        if (this.$isBlank(params)) {
          params = {}
        }
        this.tableLoading = true
        getAppingTasksPagerModel({ entity: params, query: { pageNum: page.currentPage, pageSize: 20 } })
          .then(response => {
            this.tableData = response.data.data.rows
            this.page.total = response.data.data.total
            // setTodoNum
            this.$emit('setTodoNum', this.page.total)
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        if (!this.$isBlank(form.dateScope)) {
          form.startTime = form.dateScope[0]
          form.endTime = form.dateScope[1]
        }
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      approvalClick(row, type, size) {
        // 配置关联文档 url跳转
        this.$router.push({ path: row.formUrl.replace(':id', row.formId), query: { taskId: row.taskId } })
      }
    }
  }
</script>
